export default class Lock {
  activePromise: Promise<Key>
  constructor() {
    this.activePromise = Promise.resolve(new Key())
  }
  async lock() {
    const e = new Promise<Key>(resolve => {
      this.activePromise.then(key => key.onUnlock(() => resolve(new Key())))
    })
    const t = this.activePromise
    this.activePromise = e
    return t
  }
}
class Key {
  unlockCB: Function | undefined
  onUnlock(e: Function) {
    this.unlockCB = e
  }
  unlock() {
    this.unlockCB?.()
  }
}
